#!/usr/bin/env python
# # -*- coding: utf-8 -*-

import ctypes
import hashlib
import os
import random
import socket
import sys
import threading
import time
from multiprocessing import Process, Queue
from threading import Timer
from tkinter import *

# import sesson1.run as session1
import sesson2
import sesson2.subject1
import sesson2.subject1.session2_1_main as sesson2_1
import sesson2.subject2
import sesson2.subject2.sesson2_2_main as sesson2_2

# q = Queue()
Objdll1 = ctypes.cdll.LoadLibrary("./sesson1/DLLtest.dll")
s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("path", os.path.dirname(__file__)+"/sesson2/subject2")
# sys.path.append(os.path.dirname(__file__))


def sesson2_button2_process(arg):

    ret = sesson2_2.sesson2_2_main()
    if (ret == True):
        print("运行成功")
    else:
        print("运行失败")
    print(arg)


def sesson2_button1_process(arg):
    ret = sesson2_1.session2_1_main()
    if (ret == True):
        print("运行成功")
    else:
        print("运行失败")
    print(arg)


def sesson1_button1_process(arg, Objdll, s):
    print("等待平台指令")
    session1.session1_main(Objdll, s)
    # Objdll1 = ctypes.cdll.LoadLibrary("./sesson1/DLLtest.dll")
    # s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # session1.preprocess(Objdll1, s1)
    # print("等待平台指令")
    # session1.session1_main(Objdll1, s1)
    print("运行成功")


def sesson1_button2_process(arg, Objdll, s):
    print("等待平台指令")
    session1.session1_main(Objdll, s)
    # Objdll = ctypes.cdll.LoadLibrary("./sesson1/DLLtest.dll")
    # s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # session1.preprocess(Objdll, s1)
    # print("等待平台指令")
    # session1.session1_main(Objdll, s1)
    print("运行成功")


class MY_GUI():
    def __init__(self, init_window_name):
        self.init_window_name = init_window_name

    # 设置窗口

    def set_init_window(self):
        self.init_window_name.title("东北大学 参赛队客户端 V1.00")
        # 290 160为窗口大小，+10 +10 定义窗口弹出时的默认展示位置
        self.init_window_name.geometry('1920x1080+0+0')
        self.init_window_name.geometry('1720x800+0+0')
        # self.init_window_name["bg"] = "pink"                                    #窗口背景色，其他背景色见：blog.csdn.net/chl0000/article/details/7657887
        # self.init_window_name.attributes("-alpha",0.9)                          #虚化，值越小虚化程度越高
        # 标签
        self.sesson1_RV = IntVar()
        self.sesson1_RV.set(3)
        self.init_data_label = Label(self.init_window_name, text="东北大学 参赛队客户端 V1.00",  font=(
            'Arial', 32))
        self.init_data_label.grid(row=4, column=2)

        self.init_data_label2 = Label(
            self.init_window_name, text="科目一：",  font=('Arial', 32))
        self.init_data_label2.grid(row=5, column=0)

        self.init_data_label3 = Label(
            self.init_window_name, text="情景一：",  font=('Arial', 32), width=10, height=2)
        self.init_data_label3.grid(row=5, column=1)
        self.init_data_label4 = Label(
            self.init_window_name, text="情景二：",  font=('Arial', 32), width=10, height=2)
        self.init_data_label4.grid(row=6, column=1)
        self.pr_button1 = Button(
            self.init_window_name, text="准备", bg="lightblue", width=10, command=self.preparation, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.pr_button1.grid(row=5, column=2)
        self.submit_button1 = Button(
            self.init_window_name, text="提交答案", bg="lightblue", width=10, command=self.sesson1_button1_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.submit_button1.grid(row=5, column=3)
        self.pr_button1 = Button(
            self.init_window_name, text="准备", bg="lightblue", width=10, command=self.preparation, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.pr_button1.grid(row=6, column=2)
        self.submit_button1 = Button(
            self.init_window_name, text="提交答案", bg="lightblue", width=10, command=self.sesson1_button2_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.submit_button1.grid(row=6, column=3)

        self.sesson2_label = Label(
            self.init_window_name, text="科目二：",  font=('Arial', 32), width=15, height=2)
        self.sesson2_label.grid(row=7, column=0)
        self.sesson2_labe2 = Label(
            self.init_window_name, text="情景一：",  font=('Arial', 32), width=10, height=2)
        self.sesson2_labe2.grid(row=7, column=1)
        self.init_data_label4 = Label(
            self.init_window_name, text="情景二：",  font=('Arial', 32), width=10, height=2)
        self.init_data_label4.grid(row=8, column=1)
        self.sesson2_button1 = Button(
            self.init_window_name, text="准备", bg="lightblue", width=10, command=self.test_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson2_button1.grid(row=7, column=2)
        self.sesson2_button2 = Button(
            self.init_window_name, text="提交答案", bg="lightblue", width=10, command=self.sesson2_button1_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson2_button2.grid(row=7, column=3)
        self.sesson2_button1 = Button(
            self.init_window_name, text="准备", bg="lightblue", width=10, command=self.test_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson2_button1.grid(row=8, column=2)
        self.sesson2_button2 = Button(
            self.init_window_name, text="提交答案", bg="lightblue", width=10, command=self.sesson2_button2_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson2_button2.grid(row=8, column=3)

        self.sesson2_label = Label(
            self.init_window_name, text="科目三：",  font=('Arial', 32), width=15, height=2)
        self.sesson2_label.grid(row=9, column=0)
        self.sesson2_labe2 = Label(
            self.init_window_name, text="情景一：",  font=('Arial', 32), width=10, height=2)
        self.sesson2_labe2.grid(row=9, column=1)
        self.init_data_label4 = Label(
            self.init_window_name, text="情景二：",  font=('Arial', 32), width=10, height=2)
        self.init_data_label4.grid(row=10, column=1)
        self.sesson3_button1 = Button(
            self.init_window_name, text="准备", bg="lightblue", width=10, command=self.test_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson3_button1.grid(row=9, column=2)
        self.sesson3_button2 = Button(
            self.init_window_name, text="提交答案", bg="lightblue", width=10, command=self.sesson3_button1_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson3_button2.grid(row=9, column=3)
        self.sesson3_button3 = Button(
            self.init_window_name, text="准备", bg="lightblue", width=10, command=self.test_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson3_button3.grid(row=10, column=2)
        self.sesson3_button4 = Button(
            self.init_window_name, text="提交答案", bg="lightblue", width=10, command=self.sesson3_button2_fun, font=('Arial', 32))  # 调用内部方法  加()为直接调用
        self.sesson3_button4.grid(row=10, column=3)

        self.time_text = StringVar()
        self.time_text.set(self.get_current_time())
        self.time_lable = Label(self.init_window_name, textvariable=self.time_text,  font=(
            'Arial', 24), width=24, height=4).grid(row=12, column=2)

        self.t = Timer(1, self.show_time)
        self.t.start()

    # 功能函数
    def test_fun(self):
        print("test_fun")

    def preparation(self):
        global Objdll1, s1
        res = session1.preprocess(Objdll1, s1)
        if res:
            print("准备完成")
        else:
            print("准备失败")

    def sesson3_button1_fun(self):
        print("sesson3_button1_fun")

    def sesson3_button2_fun(self):
        print("sesson3_button2_fun")

    def sesson2_button1_fun(self):
        arg = "sesson2_button1_fun"
        proc = Process(target=sesson2_button1_process, args=(arg,))
        proc.start()
        proc.join()

    def sesson2_button2_fun(self):
        arg = "sesson2_button2_fun"
        proc = Process(target=sesson2_button2_process, args=(arg,))
        proc.start()
        proc.join()

    def sesson1_button1_fun(self):
        arg = "sesson1_button1_fun"
        global Objdll1, s1
        proc = threading.Thread(target=sesson1_button1_process, args=(arg, Objdll1, s1))
        proc.start()
        # proc = Process(target=sesson1_button1_process, args=(arg, q, s1))
        # proc.start()
        # proc.join()

    def sesson1_button2_fun(self):
        arg = "sesson1_button2_fun"
        global Objdll1, s1
        proc = threading.Thread(target=sesson1_button2_process, args=(arg, Objdll1, s1))
        proc.start()
        # proc = Process(target=sesson1_button2_process, args=(arg,))
        # proc.start()
        # proc.join()

    def print_time(threadName, delay):
        count = 0
        while count < 5:
            time.sleep(delay)
            count += 1
            print("%s: %s" % (threadName, time.ctime(time.time())))
    # 获取当前时间

    def show_time(self):
        self.time_text.set(self.get_current_time())
        self.t.cancel()
        self.t = Timer(1, self.show_time)
        self.t.start()

    def get_current_time(self):
        current_time = time.strftime(
            '%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        return current_time


def gui_start():
    init_window = Tk()  # 实例化出一个父窗口
    ZMJ_PORTAL = MY_GUI(init_window)
    # 设置根窗口默认属性
    ZMJ_PORTAL.set_init_window()
    init_window.mainloop()  # 父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示


if __name__ == '__main__':
    gui_start()
